class Api::V1::ForcesController < Api::V1::BaseController

	prepend_before_filter :signed_in_user
	before_action :set_force, only: [:show, :edit, :update, :destroy]
	before_action :find_concern, only: [:index]

	def index
		if params[:concern_id]
			@forces = @concern.forces
		else
			@forces = Force.all
		end
	end

	def create
		@force = Force.new(force_params)
		if @force.save
			render action: "show"
		else
			render :status => 406,
					:json => {
						:status_code => 406,
						:message => "Error creating resource!",
						:content => @force.errors.full_messages
					}
		end
	end

	def show
	end

	def update
		if @force.update(force_params)
			render :status => 200,
					:json => { 
						:status_code => 200,
						:message => "Resource updated succesfully!",
						:content => ""
					 }
		else
			render :status => 406, 
					:json => {
						:status_code => 406,
						:message => "Error updating resource!"
						:content => @force.errors.full_messages
					 }
		end
	end

	def destroy
		if @force.destroy
			render :status => 200,
					:json => { 
						:status_code => 200,
						:message => "Resource deleted succesfully!",
						:content => ""
					}
		else
			render :status => 406, 
					:json => {
						:status_code => 406,
						:message => "Error deleting resource!",
						:content => @force.errors.full_messages
					}
		end
	end

	private

		def set_force
     		@force = Force.find(params[:id])
     		rescue ActiveRecord::RecordNotFound
				error = "The resource that you were looking for could not be found."
				render :status => 404, 
						:json => { 
							:status_code => 404,
							:message => error,
							:content => ""
						 }
   		end

		def find_concern
			if params[:concern_id]
				@concern = Concern.find(params[:concern_id])
			end
			rescue ActiveRecord::RecordNotFound
				error = "The resource that you were looking for could not be found."
				render :status => 404, 
						:json => { 
							:status_code => 404,
							:message => error,
							:content => ""
						 }
		end

		def force_params
			params.require(:force).permit(:concern_id, :name)
		end
end
